package org.ru.mse10.cvis.web.bean.action.cv;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import org.ru.mse10.cvis.entity.cv.custom.CustomField;
import org.ru.mse10.cvis.entity.util.CustomFieldType;
import org.ru.mse10.cvis.entity.util.CustomFieldValueProviderType;
import org.ru.mse10.cvis.service.CustomFieldsService;
import org.ru.mse10.cvis.web.bean.BaseFormBean;
import org.ru.mse10.cvis.web.model.CustomFieldValueProviderUIModel;

/**
 * The Class CustomFieldsAction.
 */
@SessionScoped
@ManagedBean(name = "customFieldsAction")
public class CustomFieldsAction extends
		BaseFormBean<CustomField, CustomFieldsService> {

	/** The service. */
	@EJB
	private CustomFieldsService service;

	/** The fixed action div. */
	private boolean fixedActionDiv;

	/** The value provider model. */
	private CustomFieldValueProviderUIModel valueProviderModel;

	/**
	 * Inits the.
	 * 
	 * @return the string
	 */
	public String init() {
		return "managecf";
	}

	/**
	 * Action create custom field.
	 * 
	 * @return the string
	 */
	public String actionCreateCustomField() {
		setTargetEntity(new CustomField());
		setValueProviderModel(new CustomFieldValueProviderUIModel(
				getTargetEntity()));
		setReadonly(false);
		return "customFieldDetails";
	}

	/**
	 * Load for preview.
	 * 
	 * @param customField
	 *            the custom field
	 * @return the string
	 */
	public String loadForPreview(CustomField customField) {
		setTargetEntity(customField);
		setReadonly(true);
		return "customFieldDetails";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ru.mse10.cvis.web.bean.BaseFormBean#actionSave()
	 */
	@Override
	public void actionSave() {
		super.actionSave();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ru.mse10.cvis.web.bean.BaseFormBean#actionEdit()
	 */
	@Override
	public void actionEdit() {
		super.actionEdit();
	}

	/**
	 * Action test ws data provider.
	 */
	public void actionTestWsDataProvider() {
		// WSInvoker.invokeWSDataProvider(getWsValueDataProvider());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ru.mse10.cvis.web.bean.BaseFormBean#getService()
	 */
	@Override
	protected CustomFieldsService getService() {
		return service;
	}

	/**
	 * Requires data provider.
	 * 
	 * @return true, if successful
	 */
	public boolean requiresDataProvider() {
		return CustomFieldType.SELECT_ONE == getTargetEntity().getFieldType()
				|| CustomFieldType.MULTISELECT == getTargetEntity()
						.getFieldType();

	}

	/**
	 * Gets the custom field types as select items.
	 * 
	 * @return the custom field types as select items
	 */
	public List<SelectItem> getCustomFieldTypesAsSelectItems() {
		List<SelectItem> items = new ArrayList<SelectItem>();
		for (CustomFieldType type : CustomFieldType.values()) {
			items.add(new SelectItem(type, type.getLabel()));
		}
		return items;
	}

	/**
	 * Gets the custom field value provider types as select items.
	 * 
	 * @return the custom field value provider types as select items
	 */
	public List<SelectItem> getCustomFieldValueProviderTypesAsSelectItems() {
		List<SelectItem> items = new ArrayList<SelectItem>();
		for (CustomFieldValueProviderType type : CustomFieldValueProviderType
				.values()) {
			items.add(new SelectItem(type, type.getLabel()));
		}
		return items;
	}

	/**
	 * Gets the value provider model.
	 * 
	 * @return the value provider model
	 */
	public CustomFieldValueProviderUIModel getValueProviderModel() {
		return valueProviderModel;
	}

	/**
	 * Sets the value provider model.
	 * 
	 * @param valueProviderModel
	 *            the new value provider model
	 */
	public void setValueProviderModel(
			CustomFieldValueProviderUIModel valueProviderModel) {
		this.valueProviderModel = valueProviderModel;
	}

	/**
	 * Checks if is fixed action div.
	 * 
	 * @return true, if is fixed action div
	 */
	public boolean isFixedActionDiv() {
		return fixedActionDiv;
	}

	/**
	 * Sets the fixed action div.
	 * 
	 * @param fixedActionDiv
	 *            the new fixed action div
	 */
	public void setFixedActionDiv(boolean fixedActionDiv) {
		this.fixedActionDiv = fixedActionDiv;
	}

}
